<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.name" placeholder="用户名" />
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchObj.level" clearable placeholder="用户身份">
          <el-option :value="1" label="房东" />
          <el-option :value="2" label="租客" />
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >

      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="userName" label="用户名" width="80" />

      <el-table-column prop="userPassword" label="密码" />
      <el-table-column prop="realName" label="真实姓名" />

      <el-table-column prop="userTel" label="联系方式" width="160" />
      <el-table-column prop="userIdcard" label="身份证" />

      <el-table-column prop="userSex" label="性别" width="60" />
      <el-table-column prop="permission" label="用户登录权限">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.permission"
            active-text="开启"
            inactive-text="禁止"
            @change="updatePermission(scope.row.permission,scope.row.userId)"
          />

        </template>

      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/user/edit/'+scope.row.userId">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import user from '@/api/rental/user'
// import skrollr from '@/views/edu/teacher'
export default {
  data() {
    return {
      listLoading: true, // 是否显示loading
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 3, // 每页记录数
      searchObj: {} // 查询条件
    }
  },
  created() { // 当页面加载时获取数据
    this.fetchData()
    // console.log('djskaj')
    // user.getPageList().then(response => {
    //   console.log(response.data)
    //   this.list = response.data
    //   this.listLoading = false
    // })
  },
  // mounted() {
  //   this.$nextTick(() => {
  //     TagClound()
  //   })
  // },
  // mounted(){
  //   window.addEventListener('load',function(){
  //     console.log("widow onload");
  //     skrollr.init({
  //       smoothScrolling:false,
  //       mobileDeceleration:0.004
  //     });
  //   })
  //   console.log('mounted')
  // },
  methods: {
    fetchData(page = 1) { // 调用api层获取数据库中的数据
      console.log('加载列表' + page)
      // 当点击分页组件的切换按钮时，会传输一个当前的页码的参数page
      // 解决分页无效问题
      this.page = page
      this.listLoading = true
      console.log(this.searchObj.name)
      user.getPageList(this.page, this.limit, this.searchObj).then(response => {
        console.log(response)
        if (response.code === 20000) {
          this.list = response.data.data
          this.total = response.data.record
        }
        this.listLoading = false
      })
    },
    // 清空方法
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 删除
    removeDataById(id) {
      // 弹框
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除api
        return user.removeById(id)
      }).then(() => { // 如果上一个then成功则执行此处的then回调
        this.fetchData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }
      })
    },
    updatePermission(permis, userId) {
      console.log(permis + '  ' + userId)
      user.updateUserPermission(permis, userId).then(response => {
        if (response.code === 20000) {
          this.$message({
            type: 'success',
            message: response.message
          })
        }
      })
    }
  }
}

</script>
